//
//  WKStatusOriginalView.swift
//  WKWeibo
//
//  Created by 恋guang年 on 16/3/30.
//  Copyright © 2016年 恋guang年. All rights reserved.
//

import UIKit
import SnapKit

class WKStatusOriginalView: UIView {
    /// MARK: - 微博属性
    var originalViewButtomConstraint: Constraint!
    var status: WKStatus? {
        didSet {
            // 当别人来设置微博模型的时候,我们根据微博模型来设置原创微博的相关内容
            //1.微博头像 可选绑定
            if let imageURL = status?.user?.profile_image_url {
                let url = NSURL(string: imageURL)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named:"avatar"))
            }
            /// 会员等级 在view中判断数据不合适,要在模型中判断好,直接提供图片给view,不然会影响性能
            //            if let mbrank = status?.user?.mbrank {
            //                if mbrank > 0 && mbrank <= 6 {
            //                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
            //                } else {    // 防止cell复用,没有会员的就不要等级
            //                    memberView.image = nil
            //                }
            //            }
            memberView.image = status?.user?.mbrankImage
            
            /// 认证图标
            //            if let verified_type = status?.user?.verified_type {
            //                var image: UIImage?
            //
            //                switch verified_type {
            //                case 0:
            //                    image = UIImage(named: "avatar_vip")
            //                case 2,3,5:
            //                    image = UIImage(named: "avatar_enterprise_vip")
            //                case 220:
            //                    image = UIImage(named: "avatar_grassroot")
            //                default:
            //                    image = nil
            //                }
            //
            //                verifiedView.image = image
            //            }
            verifiedView.image = status?.user?.verifiedTvpeImage
            
            //2.用户名字
            nameLabe.text = status?.user?.screen_name
            //时间
            timeLabel.text = status?.created_at
            //来源
            sourceLabel.text = status?.source
            //原创微博内容
            contentLabel.text = status?.text
            
            //原创微博配图
            //卸载originalView底部约束
            self.originalViewButtomConstraint.uninstall()
//            如果微博模型中微博图片数组有值 原创View底部参照配图底部 否则 参照contentLabel底部
            if let imageURLs = status?.storeImageURLs {
                //配图视图有图 把URL数组传给配图视图 让其处理 确定自身大小
                pictureView.imageURLs = imageURLs
                pictureView.hidden = false
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewButtomConstraint = make.bottom.equalTo(pictureView).offset(WKStatusCellMargin).constraint
                })
                
            }else{
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewButtomConstraint = make.bottom.equalTo(contentLabel).offset(WKStatusCellMargin).constraint
                })
            }
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        ///  添加子控件并约束
        prepareForUI()
    }
    /// 添加子控件并约束
    private func prepareForUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabe)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedView)
        addSubview(memberView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //添加约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(WKStatusCellMargin)
            make.top.equalTo(self).offset(WKStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
            //用户名称
            nameLabe.snp_makeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(iconView.snp_trailing).offset(WKStatusCellMargin)
                make.top.equalTo(iconView)
                
            })
            
            //时间
            timeLabel.snp_makeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(iconView.snp_trailing).offset(WKStatusCellMargin)
                make.bottom.equalTo(iconView.snp_bottom)
            })
            //微博来源
            sourceLabel.snp_makeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(timeLabel.snp_trailing).offset(WKStatusCellMargin)
                make.centerY.equalTo(timeLabel.snp_centerY)
            })
            //认证图标
            verifiedView.snp_makeConstraints(closure: { (make) -> Void in
                make.centerX.equalTo(iconView.snp_trailing)
                make.centerY.equalTo(iconView.snp_bottom)
                make.size.equalTo(CGSize(width: 17, height: 17))
            })
            //用户等级
            memberView.snp_makeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(nameLabe.snp_trailing).offset(WKStatusCellMargin)
                make.centerY.equalTo(nameLabe.snp_centerY)
            })
            //原创微博的内容
            contentLabel.snp_makeConstraints(closure: { (make) -> Void in
                make.top.equalTo(iconView.snp_bottom).offset(WKStatusCellMargin)
                make.leading.equalTo(self).offset(WKStatusCellMargin)
                make.trailing.equalTo(self).offset(-WKStatusCellMargin)
            })
            //配图
            pictureView.snp_makeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(self).offset(WKStatusCellMargin)
                make.top.equalTo(contentLabel.snp_bottom).offset(WKStatusCellMargin)
                //先确定一个大小方便调试 后续又图片数量 确定其大小
                make.size.equalTo(CGSize(width: 100, height: 100))
            })
            /// 原创View的底部参照配图pictureView的底部
            self.snp_makeConstraints(closure: { (make) -> Void in
                //记录originalView底部约束
                self.originalViewButtomConstraint = make.bottom.equalTo(pictureView).offset(WKStatusCellMargin).constraint
            })
            
        }
    }
    //MARK: - 懒加载子控件
    ///用户头像
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar"))
    /// 用户名称
    private lazy var nameLabe:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
    ///  时间
    private lazy var timeLabel:UILabel = {
        let timeLabel = UILabel()
        timeLabel.font = UIFont.systemFontOfSize(9)
        timeLabel.textColor = UIColor.blackColor()
        return timeLabel
    }()
    ///  来源
    private lazy var sourceLabel:UILabel = {
        let sourceLabel = UILabel()
        sourceLabel.textColor = UIColor.blackColor()
        sourceLabel.font = UIFont.systemFontOfSize(9)
        return sourceLabel
    }()
    ///  会员等级
    private lazy var memberView:UIImageView = UIImageView()
    ///  认证图标
    private lazy var verifiedView:UIImageView = UIImageView()
    ///  原创微博内容
    private lazy var contentLabel:UILabel = {
        let contentView = UILabel()
        contentView.text = "sjdkfkdjsdfjsjfidjfdkfjlsjfiowjfiejfw"
        contentView.font = UIFont.systemFontOfSize(WKStatusContentDefaultSize)
        contentView.numberOfLines = 0
        contentView.textColor = UIColor.blackColor()
        return contentView
    }()
    /// 配图
    private lazy var pictureView:WKStatusPictureView = WKStatusPictureView()
}



















